package com.rf.richfitwheel.workflow.controller;

import com.rf.richfitwheel.common.utils.PageUtils;
import com.rf.richfitwheel.common.utils.R;
import com.rf.richfitwheel.workflow.model.WfAuthRuleField;
import com.rf.richfitwheel.workflow.model.WfProcessPage;
import com.rf.richfitwheel.workflow.service.WfAuthRuleFieldService;
import com.rf.richfitwheel.workflow.service.WfProcessPageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 流程目录表
 *
 * @author zhanglu
 * @email zhanglu2@cnpc.com.cn
 * @date 2021-07-20 09:01:22
 */
@RestController
@RequestMapping("/wfauthrulefield")
public class WfAuthRuleFieldController {
    @Autowired
    private WfAuthRuleFieldService wfAuthRuleFieldService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = wfAuthRuleFieldService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/listByFolderCode")
    public R list(@RequestBody String folderCode) {
        List<WfAuthRuleField> wfAuthRuleFields = wfAuthRuleFieldService.getListByFolderCode(folderCode);

        return R.ok().put("wfAuthRuleFields", wfAuthRuleFields);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") String id) {
        WfAuthRuleField wfAuthRuleField = wfAuthRuleFieldService.getById(id);

        return R.ok().put("wfAuthRuleField", wfAuthRuleField);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody WfAuthRuleField wfAuthRuleField) {
        if (wfAuthRuleField.getId() == null || "".equals(wfAuthRuleField.getId())) {
            boolean checkRuleField = wfAuthRuleFieldService.checkRuleField(wfAuthRuleField.getFolderCode(), wfAuthRuleField.getRuleField());
            if(checkRuleField){
                return R.error().put("msg", "规则属性已经存在");
            }
            //新增
            wfAuthRuleFieldService.addWfAuthRuleField(wfAuthRuleField);
        } else {
            //修改
            wfAuthRuleFieldService.updateWfAuthRuleField(wfAuthRuleField);
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody WfAuthRuleField wfAuthRuleField) {
        wfAuthRuleFieldService.updateWfAuthRuleField(wfAuthRuleField);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/deleteByIds")
    public R delete(@RequestBody String[] ids) {
        wfAuthRuleFieldService.deleteByIds(Arrays.asList(ids));

        return R.ok();
    }

    @PostMapping("/deleteById")
    public R deleteById(@RequestBody String id) {
        wfAuthRuleFieldService.deleteById(id);
        return R.ok();
    }

}
